package com.yj.service.impl;

import com.yj.entity.Region;
import com.yj.dao.RegionMapper;
import com.yj.service.IRegionService;
import com.yj.util.PageUtil;
import com.yj.vo.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yj
 * @since 2020-08-24
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

	@Autowired
	RegionMapper region;
	
	@Override
	public JsonResult AllProvince() {
		JsonResult ret = null;
		List<String> allprovnice = region.allprovnice();
		if(allprovnice!=null&&allprovnice.size()!=0) {
			ret = new JsonResult(true, "省份查找成功");
			ret.setData("provnice", allprovnice);
			return ret;
		}else {
			ret = new JsonResult(false, "省份查询失败");
			return ret;
		}
	}

	@Override
	public JsonResult Allcity(String province) {
		System.out.println(province);
		JsonResult ret = null;
		List<String> findcityBypro = region.findcityBypro(province);
		if(findcityBypro!=null&&findcityBypro.size()!=0) {
			ret = new JsonResult(true, "市查询成功");
			ret.setData("city", findcityBypro);
			return ret;
		}else {
			ret = new JsonResult(false, "市查询失败");
			return ret;
		}
	}

	@Override
	public JsonResult Houcity() {
		int n = 1;
		JsonResult ret = null;
		QueryWrapper<Region> wrapper = new QueryWrapper<Region>();
		wrapper.apply("region.did in (SELECT DISTINCT h.did from houses h where h.did in (select hid from details GROUP BY hid ORDER BY COUNT(hid) DESC))");
		List<Region> selectList = region.selectList(wrapper);
		if(selectList!=null) {
			Map<String,Map<String,String>> citymap = new HashMap<String,Map<String,String>>();
			for(Region re : selectList) {
				String prov = re.getProvince();
				String city = re.getCity();
				Map<String,String> map = new HashMap<String,String>();
				map.put("province", prov);
				map.put("city", city);
				citymap.put("city"+n++, map);
			}
			ret = new JsonResult(true, "热门城市查询成功");
			ret.setData("citys", citymap);
			return ret;
		}else {
			ret = new JsonResult(false, "热门城市查询失败");
			return ret;
		}
	}

	@Override
	public JsonResult delRegion(int did) {
		JsonResult ret = null;
		int num= region.deleteById(did);
		if(num>0) {
			ret = new JsonResult(true, "删除成功");
			return ret;
		}else {
			ret = new JsonResult(false, "删除失败");
			return ret;
		}
	}
	
	
	//地区表的更新
	@Override
	public JsonResult update(Region region) {
		JsonResult jsonResult = new JsonResult(false, null);
		int num = baseMapper.updateById(region);
		if (num > 0) {
			jsonResult.setMessage("更新成功");
			jsonResult.setSuccess(true);
		} else {
			jsonResult.setMessage("更新失败");
		}
		return jsonResult;
	}
	
	
	@Override
	public JsonResult addRegion(Region region) {
		JsonResult jsonResult=new JsonResult(false, null);
		int num=baseMapper.insert(region);
		if(num>0) {
			jsonResult.setMessage("添加信息成功");
			jsonResult.setSuccess(true);
		}else {
			jsonResult.setMessage("添加信息失败");
		}
		return jsonResult;
	}

	@Override
	public JsonResult pages() {
		JsonResult jsonResult=PageUtil.pages(region, null, 6);
		return jsonResult;
	}

	@Override
	public JsonResult page(int pageNum) {
		JsonResult jsonResult=PageUtil.page(pageNum, 6, region, null);
		return jsonResult;
	}
	

}
